<?php
/** 
*
* @package allytool-mod
* $Id: acp_allytool.php 1 2010-01-24 08:49:05Z allytools $
* @license http://opensource.org/licenses/gpl-license.php GNU Public License*
* includes/acp/acp_allytool.php
*

/*
 * @ignore 
*/ 
if (!defined('IN_PHPBB')) 
{ 
	exit; 
}

/**
*
*/
class acp_allytool
{
	var $u_action;

	function main($id, $mode)
	{
		global $db, $user, $auth, $template, $cache;
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;

		$user->add_lang('mods/acp_allytool');    

		include($phpbb_root_path . 'allytool/includes/functions_allytool.' . $phpEx);

		//Select the template file
		$this->tpl_name = 'acp_allytool';

		// Salting the form...yumyum ...
		add_form_key('acp_allytool');
		
		// What are we working on?
		switch ($mode)
		{
			case 'settings':
				$mod_id				= request_var('mod_id',				0	);
				$server				= request_var('server',				0	);
				$uni				= request_var('uni',				0	);
				$restrict_to		= request_var('restrict_to',		0	);
				

				$lang = 'ACP_ALLYTOOL_SETTINGS';

				$this->page_title = $lang;

				// Get all config data
				$allytool_config = get_allytool_config();

				// Pull all config data
				$sql = 'SELECT * 
					FROM ' . ALLYTOOL_CONFIG_TABLE;
				$result = $db->sql_query($sql);
				while( $row = $db->sql_fetchrow($result) )
				{
					$config_name = $row['config_name'];
					$config_value = $row['config_value'];
					$default_config[$config_name] = isset($_POST['submit']) ? str_replace("'", "\'", $config_value) : $config_value;
					$new[$config_name] = request_var( $config_name , $default_config[$config_name]);

					if ( isset($_POST['submit']) )
					{
						// Is it salty ?
						if (!check_form_key('acp_allytool'))
						{
							trigger_error('FORM_INVALID');
						}
						
						$sql_ary = array(
							'config_value'		=> $new[$config_name],
						);
						$sql = 'UPDATE ' . ALLYTOOL_CONFIG_TABLE . ' 
							SET ' . $db->sql_build_array('UPDATE', $sql_ary) . " 
							WHERE config_name = '$config_name'" ;
						$db->sql_query($sql);
					}
				}
				$db->sql_freeresult($result);
				
				if ( isset($_POST['submit']) )
				{
					add_log('admin', 'LOG_ALLYTOOL_SETTINGS');
					$error = $user->lang[$lang . '_UPDATED'];
					trigger_error($error . adm_back_link($this->u_action));
				}
				
				// Init some vars
				$combo_mod_entries 		= '';

				$tool_on_yes 		= ( $new['tool_on']) ? "checked=\"checked\"" : "";
				$tool_on_no 		= ( !$new['tool_on']) ? "checked=\"checked\"" : "";


				// Get all group data
				$combo_groups_entries = '';
				$sql = 'SELECT * 
					FROM ' . GROUPS_TABLE . ' 
					WHERE group_type <> ' . GROUP_SPECIAL ;
				$result = $db->sql_query($sql);
				while ($row = $db->sql_fetchrow($result))
				{
					$selected = ( $row['group_id'] == $allytool_config['restrict_to'] ) ? 'selected' : '';
					$combo_groups_entries .= '<option value="' . $row['group_id'] . '" ' . $selected . '>' . $row['group_name'] . '</option>';
				}
				$db->sql_freeresult($result);

				// Generate groups combobox
				$selected = ( $allytool_config['restrict_to'] == 0 ) ? 'selected' : '';
				$group_combo	 = '<select name="restrict_to">';
				$group_combo	.= '<option value="0" ' . $selected . '>' . $user->lang[$lang . '_DEACTIVATED'] . '</option>';
				$group_combo	.= $combo_groups_entries;
				$group_combo	.= '</select>';
				
				// Get all server data
				$combo_server_entries = '';
				$sql = 'SELECT * 
					FROM ' . ALLYTOOL_SERVER_TABLE . '';
				$result = $db->sql_query($sql);
				while ($row = $db->sql_fetchrow($result))
				{
					$selected = ( $row['server_id'] == $allytool_config['server'] ) ? 'selected' : '';
					$combo_server_entries .= '<option value="' . $row['server_id'] . '" ' . $selected . '>' . $row['server_name'] . '</option>';
				}
				$db->sql_freeresult($result);
				
				// Generate server combobox
				$selected = ( $allytool_config['server'] == 0 ) ? 'selected' : '';
				$server_combo	 = '<select name="server">';
				$server_combo	.= '<option value="0" ' . $selected . '>' . $user->lang[$lang . '_DEACTIVATED'] . '</option>';
				$server_combo	.= $combo_server_entries;
				$server_combo	.= '</select>';

				$template->assign_vars(array(
					'D_ACCESS_GROUP'					=> $group_combo,
					'D_SERVER'							=> $server_combo,
					'S_SETTING'							=> true,
					'S_TOOL_ON_YES'						=> $tool_on_yes,
					'S_TOOL_ON_NO'						=> $tool_on_no,
					'ACP_ALLYTOOL_SETTING_TOOL_ON'		=> $new['tool_on'],
					'ACP_ALLYTOOL_SETTING_SERVER'		=> $allytool_config['server'],
					'ACP_ALLYTOOL_SETTING_UNI'			=> $allytool_config['uni'],
					'ALLYTOOL_MOD_RELEASE' 				=> sprintf($user->lang['ACP_ALLYTOOL_COPY'], $allytool_config['allytool_version'])
				));
				
				if ( is_dir( $phpbb_root_path . 'install_allytool/' ) === TRUE )
				{
					if ( is_file( $phpbb_root_path . 'install_allytool/install.'.$phpEx ) === TRUE )
					{
						include $phpbb_root_path . 'install_allytool/install.'.$phpEx;

						if ( version_compare( $current_version, $allytool_config['allytool_version'], '<=' ) === TRUE )
						{
							$template->assign_vars(array(
								'S_DISPLAY_GENERAL'	=> true,
								'GEN_TITLE'				=> $user->lang['ACP_ALLYTOOL_ERROR'],
								'GEN_MESSAGE'			=> sprintf( $user->lang['ACP_ALLYTOOL_DELETE_DIR'], $phpbb_root_path . 'install_allytool' )
							));
						}
						else
						{
							$template->assign_vars(array(
								'S_DISPLAY_GENERAL'	=> true,
								'GEN_TITLE'				=> $user->lang['ACP_ALLYTOOL_UPDATE'],
								'GEN_MESSAGE'			=> sprintf( $user->lang['ACP_ALLYTOOL_UPDATE_TEXT'], $phpbb_root_path . 'install_allytool/install.'.$phpEx, $current_version )
							));
						}

						$load_center = false;
					}
				}/*
				// Daten für Footer
				$template->assign_vars(array(
					'ALLYTOOL_MOD_RELEASE' => sprintf($user->lang['ACP_ALLYTOOL_COPY'], $allytool_config['allytool_version'])
					));*/
			break;

			case 'ally':
				$lang = 'ACP_ALLYTOOL_ALLY';
				$this->page_title = $lang;
				
				// Check buttons
				$button_addally 	= (isset($_POST['addally'])) 	? true : false;
				$button_add 		= (isset($_POST['add'])) 		? true : false;
				$button_del 		= (isset($_POST['del'])) 		? true : false;
				$button_edit 		= (isset($_POST['edit'])) 		? true : false;

				// Check data
				$allyname			= request_var('allyname'			,	''	,	true	);
				$allytag			= request_var('allytag'				,	'' 	);
				$allycolor			= request_var('allycolor'			,	'000000'	);
				$allyshowrangliste	= request_var('allyrang'			,	0	);
				$allyshowplaneten	= request_var('allyplanet'			,	0	);
				$allypos			= request_var('allypos'				,	0	);
				$allysort			= request_var('allysort'			,	0	);
				$ally_id			= request_var('ally_id'				,	0	);

				// Init some vars
				$allytool_config = get_allytool_config();

				// Add a new entry
				if ( $button_add && $allyname <> '' )
				{
					// Is it salty ?
					if (!check_form_key('acp_allytool'))
					{
						trigger_error('FORM_INVALID');
					}
					
					if ( $ally_id == 0 )
					{
						$sql_ary = array(
							'ally_id'				=> $ally_id,
							'ally_name'				=> $allyname,
							'ally_tag'				=> $allytag,
							'ally_color'			=> $allycolor,
							'ally_show_rangliste'	=> $allyshowrangliste,
							'ally_show_planeten'	=> $allyshowplaneten,
							'ally_pos'				=> $allypos,
							'ally_sort'				=> $allysort
						);
						$db->sql_query('INSERT INTO ' . ALLYTOOL_ALLI_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
						add_log('admin', 'LOG_ALLYTOOL_ALLY_ADD', $allyname);
					}
					else 
					{
							$sql_ary = array(
								'ally_id'				=> $ally_id,
								'ally_name'				=> $allyname,
								'ally_tag'				=> $allytag,
								'ally_color'			=> $allycolor,
								'ally_show_rangliste'	=> $allyshowrangliste,
								'ally_show_planeten'	=> $allyshowplaneten,
								'ally_pos'				=> $allypos,
								'ally_sort'				=> $allysort
							);
							$sql = 'UPDATE ' . ALLYTOOL_ALLI_TABLE . ' 
								SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
								WHERE ally_id = $ally_id";
							$db->sql_query($sql);
						
						add_log('admin', 'LOG_ALLYTOOL_ALLY_EDIT', $allyname);
					}
					$error = $user->lang[$lang . '_ALLY_UPDATED'];
					trigger_error($error . adm_back_link($this->u_action));
				}

				// Delete an entry
				if ( $button_del && $ally_id <> 0)
				{
					// Is it salty ?
					if (!check_form_key('acp_allytool'))
					{
						trigger_error('FORM_INVALID');
					}
					
					$sql = 'DELETE FROM ' . ALLYTOOL_ALLI_TABLE . " 
							WHERE ally_id = $ally_id";
					$db->sql_query($sql);

					add_log('admin', 'LOG_ALLYTOOL_ALLY_DEL', $allyname);
					$error = $user->lang[$lang . '_ALLY_DELETED'];
					trigger_error($error . adm_back_link($this->u_action));
				}

				// Load add- oder editpage
				if ( $button_addally || ( $button_edit && $ally_id <> 0 ) || ( $button_add && $allyname == '' ) )
				{
					$preselected_id = '';

					// Create error messages
					if ($button_add && $allyname == '')
					{
						$error	 = $user->lang[$lang . '_ERROR_ALLY'];
						trigger_error($error . adm_back_link($this->u_action));
					}

					// Init some vars
					$title_exp 	= $user->lang[$lang . '_TITEL_ADD_ALLY_EXPLAIN'];
					$title 		= $user->lang[$lang . '_TITEL_ADD_ALLY'];

					// Load initial values
					if ( $button_edit || ( $button_add && $allyname == '' ))
					{
						// overwrites the "add ally" title and sets the "edit ally" title
						$title_exp 	= $user->lang[$lang . '_TITEL_EDIT_ALLY_EXPLAIN']; // overwrites the "add ally" title and sets the "edit ally" title
						$title 		= $user->lang[$lang . '_TITEL_EDIT_ALLY'];

						// Get allys data
						$sql = 'SELECT * 
							FROM ' . ALLYTOOL_ALLI_TABLE . ' 
							WHERE ally_id = ' . (int) $ally_id . '
								ORDER BY ally_name';
						$result = $db->sql_query($sql);
						$row = $db->sql_fetchrow($result);
						if ( $button_edit ) 
						{
							$allyname 			= $row['ally_name'];
							$ally_id 			= $row['ally_id'];
							$allytag			= $row['ally_tag'];
							$allycolor			= $row['ally_color'];
							$allyshowplaneten	= $row['ally_show_planeten'];
							$allyshowrangliste	= $row['ally_show_rangliste'];
							$allysort			= $row['ally_sort'];
							$allypos			= $row['ally_pos'];
						}
						
						$db->sql_freeresult($result);
					}

					$template->assign_vars(array(
						'U_ACTION'					=> $this->u_action,
						'S_ADDALLY'					=> true,
						'PREDEFINED_NAME'			=> $allyname,
						'PREDEFINED_TAG'			=> $allytag,
						'PREDEFINED_COLOR'			=> $allycolor,
						'PREDEFINED_PLANET'			=> $allyshowplaneten,
						'PREDEFINED_RANG'			=> $allyshowrangliste,
						'PREDEFINED_SORT'			=> $allysort,
						'PREDEFINED_POS'			=> $allypos,
						'ALLYID'					=> $ally_id,
						'L_ACP_ALLYTOOL_ALLY_EXPLAIN'	=> $title_exp,
						'L_ACP_ALLYTOOL_ALLY'			=> $title,
						'U_SWATCH'			=> append_sid("{$phpbb_admin_path}swatch.$phpEx", 'form=settings&amp;name=PREDEFINED_COLOR'),
						)
					);
				}
				else 
				{
					// Load the ally overview page
					// Get all allydata
					$sql = 'SELECT * 
						FROM ' . ALLYTOOL_ALLI_TABLE . ' 
						ORDER BY ally_name';
					$result = $db->sql_query($sql);

					while ($row = $db->sql_fetchrow($result))
					{
						$current_user_id	= $row['ally_id'];
						
						$template->assign_block_vars('allyrow', array(
								'ALLYNAME'		=> $row['ally_name'],
								'ALLYID'		=> $row['ally_id'],
								'ALLYTAG'		=> $row['ally_tag'],
								'ALLYCOLOR'		=> $row['ally_color'],
								'ALLYRANG'		=> $row['ally_show_rangliste'],
								'ALLYPLANET'	=> $row['ally_show_planeten'],
								)
							);
						
					}
					$db->sql_freeresult($result);

					$colspan = 7;
					
					// Generate page
					$template->assign_vars(array(
						'U_ACTION'			=> $this->u_action,
						'S_ALLY'			=> true,
						'COLSPAN'			=> $colspan,
						)
					);
				}
				
				$template->assign_vars(array(
					'ALLYTOOL_MOD_RELEASE' => sprintf($user->lang['ACP_ALLYTOOL_COPY'], $allytool_config['allytool_version'])
					));				
			break;
			
			case 'user':
					
				// Get all config data
				$allytool_config = get_allytool_config();
				$enable	=	$allytool_config['restrict_to'];
				
				// Check buttons
				$button_adduser 	= (isset($_POST['adduser'])) 	? true : false;
				$button_add 		= (isset($_POST['add'])) 		? true : false;
				$button_del 		= (isset($_POST['del'])) 		? true : false;
				$button_edit 		= (isset($_POST['edit'])) 		? true : false;
				
				// Load add- oder editpage
				if ( $button_edit && $user_id <> 0  )
				{
					$preselected_id = '';

					// Create error messages
					if ($button_add && $username == '')
					{
						$error	 = $user->lang[$lang . '_ERROR_USER'];
						trigger_error($error . adm_back_link($this->u_action));
					}

					// Init some vars
					$title_exp 	= $user->lang[$lang . '_TITEL_ADD_USER_EXPLAIN'];
					$title 		= $user->lang[$lang . '_TITEL_ADD_USER'];

					// Load initial values
					if ( $button_edit || ( $button_add && $username == '' ))
					{
						// overwrites the "add ally" title and sets the "edit ally" title
						$title_exp 	= $user->lang[$lang . '_TITEL_EDIT_USER_EXPLAIN']; // overwrites the "add ally" title and sets the "edit ally" title
						$title 		= $user->lang[$lang . '_TITEL_EDIT_USER'];

						// Get allys data
						$sql = 'SELECT p.user_id, ps.username, pp.pf_ingame
						FROM ' . USER_GROUP_TABLE . ' p,
						' . USERS_TABLE .' ps,
						' . PROFILE_FIELDS_DATA_TABLE .' pp
						WHERE p.group_id=' . $enable . '
							AND ps.user_id=p.user_id AND pp.user_id=p.user_id ORDER BY user_name';
						$result = $db->sql_query($sql);
						$row = $db->sql_fetchrow($result);
						if ( $button_edit ) 
						{
							$username 			= $row['username'];
							$user_id			= $row['user_id'];
							
						}
						
						$db->sql_freeresult($result);
					}

					$template->assign_vars(array(
						'U_ACTION'					=> $this->u_action,
						'S_ADDUSER'					=> true,
						'PREDEFINED_NAME'			=> $allyname,
						'PREDEFINED_TAG'			=> $allytag,
						'PREDEFINED_COLOR'			=> $allycolor,
						'PREDEFINED_PLANET'			=> $allyshowplaneten,
						'PREDEFINED_RANG'			=> $allyshowrangliste,
						'PREDEFINED_SORT'			=> $allysort,
						'PREDEFINED_POS'			=> $allypos,
						'ALLYID'					=> $ally_id,
						'L_ACP_ALLYTOOL_ALLY_EXPLAIN'	=> $title_exp,
						'L_ACP_ALLYTOOL_ALLY'			=> $title,
						'U_SWATCH'			=> append_sid("{$phpbb_admin_path}swatch.$phpEx", 'form=settings&amp;name=PREDEFINED_COLOR'),
						)
					);
				}
				else 
				{
				// Load the user overview page
				// Get all config data
				$allytool_config    = get_allytool_config();
				$enable             = $allytool_config['restrict_to'];

				// Load the user overview page
				// Get user_id from user_group
				$sql = 'SELECT p.user_id, ps.username, pp.pf_ingame
						FROM ' . USER_GROUP_TABLE . ' p,
						' . USERS_TABLE .' ps,
						' . PROFILE_FIELDS_DATA_TABLE .' pp
						WHERE p.group_id=' . $enable . '
							AND ps.user_id=p.user_id AND pp.user_id=p.user_id';
				
				$result = $db->sql_query($sql);
				while ($row = $db->sql_fetchrow($result))
				{
					$user_id			= $row['user_id'];
					
				    $template->assign_block_vars('userrow', array(
						'USERID'          => $row['user_id'],
				        'USERNAME'          => $row['username'],
				        'USERINGAME'      => $row['pf_ingame'],
				    ));
				}
				$db->sql_freeresult($result);
				$colspan = 4;
				// Generate page
				$template->assign_vars(array(
				    'U_ACTION'         => $this->u_action,
				    'S_USER'         => true,
				    'COLSPAN'         => $colspan,
					'ALLYTOOL_MOD_RELEASE' => sprintf($user->lang['ACP_ALLYTOOL_COPY'], $allytool_config['allytool_version'])
				)); 
				}	
			break;

			default:
				$error_msg = 'You should never reach this point ;-)';
				trigger_error($error_msg);
			break;
		}
	}
}

?>